package com.dx.config;

import com.dx.intercepter.JwtIntercepter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class WebMvcConfig extends WebMvcConfigurationSupport {
    private final JwtIntercepter jwtIntercepter;
//    private final SwaggerProperties swaggerProperties;
//    @Bean
//    public Docket swaggerConfig() {
//        ApiInfo apiInfo = new ApiInfoBuilder()
//                .title(swaggerProperties.getTitle())
//                .version(swaggerProperties.getVersion())
//                .description(swaggerProperties.getDescription())
//                .build();
//        return new Docket(DocumentationType.SWAGGER_2)
//                .groupName(swaggerProperties.getGroupName())
//                .apiInfo(apiInfo)
//                .select()
//                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
//                .paths(PathSelectors.any())
//                .build();
//    }


    /**
     *  设置静态资源映射
     *  No mapping for GET /doc.html  没有设置静态资源映射，springmvc把它理解为了一个接口
     * **/


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


    //TODO 目前的策略是邮箱接口不走拦截器，但正确的做法应该是内部服务不走拦截器, 外部服务走拦截器.
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        System.out.println(" 添加拦截器 ");
        registry.addInterceptor(jwtIntercepter).
                addPathPatterns("/interface/**").
                addPathPatterns("/admin/**").
                addPathPatterns("/log/**").
                excludePathPatterns("/admin/login","/admin/register")
                .excludePathPatterns("/admin/activation","/admin/activation/verify/**")
                .excludePathPatterns("/email/send");
    }

    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 添加自定义消息转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(new JacksonObjectMapper());
        converters.add(1, converter); // 将自定义转换器插入到第一个位置
        super.extendMessageConverters(converters);
    }
}
